﻿using MXComponent.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace MXComponent.DAL
{
    public partial class userDB
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public userDB()
        {
        }

        #region 成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="userid">主键值</param>
        /// <returns>是否存在</returns>
        public bool Exists(int userid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from systemuser");
            strSql.Append(" where userid = @userid ");
            SqlParameter[] parameters = {
                    new SqlParameter("@userid", SqlDbType.Int)
                };
            parameters[0].Value = userid;

            return Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters)) != 0;
        }

        //public bool CheckLogin(string loginname, string password)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("select count(1) from systemuser");
        //    strSql.Append(" where loginname = @loginname and password=@password");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@loginname", SqlDbType.NVarChar,50),
        //             new SqlParameter("@password", SqlDbType.NVarChar,50)
        //        };
        //    parameters[0].Value = loginname;
        //    parameters[1].Value = password;
        //    return Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters)) != 0;
        //}

        public void ChangePassword(int userid, string newpassword)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"update systemuser set password=@password ");
            strSql.Append(" where userid = @userid");
            SqlParameter[] parameters = {
                    new SqlParameter("@userid", SqlDbType.Int),
                     new SqlParameter("@password", SqlDbType.NVarChar,50)
                };
            parameters[0].Value = userid;
            parameters[1].Value = newpassword;
            SqlHelper.ExecteNonQuery(CommandType.Text, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个model
        /// </summary>
        /// <param name="userid">主键值</param>
        /// <returns>model</returns>
        public systemuserData CheckLogin(string loginname, string password)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select userid,username,loginname,password,lastlogintime,createtime,creatby,updatetime,updateby,active,IsSystem from systemuser");
            strSql.Append(" where loginname = @loginname and password=@password and active=1");
            SqlParameter[] parameters = {
                    new SqlParameter("@loginname", SqlDbType.NVarChar,50),
                     new SqlParameter("@password", SqlDbType.NVarChar,50)
                };
            parameters[0].Value = loginname;
            parameters[1].Value = password;

            systemuserData model = new systemuserData();
            DataSet ds = SqlHelper.ExecuteDataSet(CommandType.Text, strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                if (row["userid"] != DBNull.Value)
                {
                    model.userid = Convert.ToInt32(row["userid"].ToString());
                }
                if (row["username"] != DBNull.Value)
                {
                    model.username = Convert.ToString(row["username"].ToString());
                }
                if (row["loginname"] != DBNull.Value)
                {
                    model.loginname = Convert.ToString(row["loginname"].ToString());
                }
                if (row["password"] != DBNull.Value)
                {
                    model.password = Convert.ToString(row["password"].ToString());
                }
                if (row["lastlogintime"] != DBNull.Value)
                {
                    model.lastlogintime = Convert.ToString(row["lastlogintime"].ToString());
                }
                if (row["createtime"] != DBNull.Value)
                {
                    model.createtime = Convert.ToString(row["createtime"].ToString());
                }
                if (row["creatby"] != DBNull.Value)
                {
                    model.creatby = Convert.ToInt32(row["creatby"].ToString());
                }
                if (row["updatetime"] != DBNull.Value)
                {
                    model.updatetime = Convert.ToString(row["updatetime"].ToString());
                }
                if (row["updateby"] != DBNull.Value)
                {
                    model.updateby = Convert.ToInt32(row["updateby"].ToString());
                }
                if (row["active"] != DBNull.Value)
                {
                    model.active = Convert.ToInt32(row["active"].ToString());
                }
                if (row["IsSystem"] != DBNull.Value)
                {
                    model.IsSystem = Convert.ToBoolean(row["IsSystem"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model</param>
        public int AddRecord(systemuserData model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("set nocount on; ");
            strSql.Append("insert into systemuser(");
            strSql.Append(@"username,loginname,password,lastlogintime,createtime,creatby,updatetime,updateby,active) ");
            strSql.Append(" values (");
            strSql.Append(@"@username,@loginname,@password,@lastlogintime,@createtime,@creatby,@updatetime,@updateby,@active)");
            strSql.Append("; select @@identity; set nocount off; ");
            SqlParameter[] parameters = {
                    new SqlParameter("@username", SqlDbType.NVarChar,50),
                    new SqlParameter("@loginname", SqlDbType.NVarChar,50),
                    new SqlParameter("@password", SqlDbType.NVarChar,50),
                    new SqlParameter("@lastlogintime", SqlDbType.DateTime),
                    new SqlParameter("@createtime", SqlDbType.DateTime),
                    new SqlParameter("@creatby", SqlDbType.Int),
                    new SqlParameter("@updatetime", SqlDbType.DateTime),
                    new SqlParameter("@updateby", SqlDbType.Int),
                    new SqlParameter("@active", SqlDbType.Int)
                };
            parameters[0].Value = model.username;
            parameters[1].Value = model.loginname;
            parameters[2].Value = model.password;
            parameters[3].Value = model.lastlogintime == string.Empty ? null : model.lastlogintime;
            parameters[4].Value = model.createtime == string.Empty ? null : model.createtime;
            parameters[5].Value = model.creatby;
            parameters[6].Value = model.updatetime == string.Empty ? null : model.updatetime;
            parameters[7].Value = model.updateby;
            parameters[8].Value = model.active;

            int id = 0;
            object ret = SqlHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);

            if (ret != null && ret != DBNull.Value)
            {
                id = model.userid;
            }
            return id;
        }

        #endregion 成员方法
    }
}